<?php
	if (!defined('BASEPATH'))
		exit('No direct script access allowed');

	class Signup extends CI_Controller
	{
		public $error_code = 0;
		public $result_message = '';
		public $email_message = '';
		public $confirm_email_message = '';
		public $confirm_password_message = '';

		public function index()
		{
			$data = array(
				'error_code' => $this->error_code,
				'result_message' => $this->result_message,
				'email_message' => $this->email_message,
				'confirm_email_message' => $this->confirm_email_message,
				'confirm_password_message' => $this->confirm_password_message,
			);
			$this->load->view('v_signup', $data);
		}

		public function addUser()
		{
			if ($_POST['Email'] != $_POST['ConfirmEmail'])
			{
				$this->error_code = -1000;
				$this->confirm_email_message = 'email validation failed';
			}
			else if ($_POST['CreatePassword'] != $_POST['ConfirmPassword'])
			{
				$this->error_code = -2000;
				$this->confirm_password_message = 'password validation failed';
			}
			else
			{
				$cryptPassword = crypt($_POST['CreatePassword']);

				$this->load->model('m_user', 'currentUser');
				$this->currentUser->set_user_email($_POST['Email']);
				$this->currentUser->set_user_password($cryptPassword);
				$this->error_code = $this->currentUser->add_user();

				if ($this->error_code == 0)
				{
					$this->load->library('email');

					$this->email->from('donotreply@cecs492.com', 'Billing Service');
					$this->email->to($_POST['Email']);
					$this->email->subject('Registration');

					$emailMessage = "Click on the following link to complete your registration.\n\n";
					$emailMessage .= 'http://' . $_SERVER["SERVER_NAME"];
					$emailMessage .= '/cecs492/prototype/billingservice/signup/confirmUser?email=';
					$emailMessage .= $_POST['Email'] . '&code=' . $cryptPassword;

					$this->email->message($emailMessage);
					$this->email->send();

					// echo $this->email->print_debugger();
					// go back to home screen
					$message = 'An email has been sent to ' . $_POST['Email'] . '. Please click on the enclosed ';
					$message .= 'link to complete the registration process.';
					$data['message'] = $message;
					$this->load->view('v_home', $data);
					return;
				}
				else if ($this->error_code == -1)
				{
					$this->email_message = 'user already exists, pick something else';
				}
				// change to a switch state if there are more than 2 error codes...

			}

			$data = array(
				'error_code' => $this->error_code,
				'result_message' => $this->result_message,
				'email_message' => $this->email_message,
				'confirm_email_message' => $this->confirm_email_message,
				'confirm_password_message' => $this->confirm_password_message,
			);
			$this->load->view('v_signup', $data);
		}

		public function confirmUser()
		{
			$this->load->model('m_user', 'registeredUser');
			$this->registeredUser->set_user_email($_GET["email"]);
			$this->registeredUser->set_user_password($_GET["code"]);
			$this->error_code = $this->registeredUser->validate_user();
			
			if($this->error_code == 0)
			{
				$data['message'] = 'Congratulations. You may now log in to start using the system.';
				$this->load->view('v_home', $data);
			}
			else
			{
				// handle errors...
			}
		}
	}
?>
